package com.xinchen.aiowaste.ui.adapter

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.maple.common.widget.view.LineView
import com.maple.model.data.MainMenuData
import com.maple.model.entity.aio.WasteListEntity
import com.maple.utils.ext.layoutInflater
import com.maple.utils.util.LogUtils
import com.maple.utils.util.UIUtils
import com.xinchen.aiowaste.R

class WasteListAdapter(val context: Context): RecyclerView.Adapter<WasteListAdapter.ViewHolder>(){

    private val list: MutableList<WasteListEntity.DataList> = mutableListOf()

    private var listener: OnClickListener? = null

    fun setListener(l: OnClickListener?) {
        listener = l
    }

    fun setData(l: List<WasteListEntity.DataList>?) {
        if(!l.isNullOrEmpty()) {
            this.list.clear()
            this.list.addAll(l)
            this.notifyDataSetChanged()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WasteListAdapter.ViewHolder {
        val view: View = context.layoutInflater.inflate(R.layout.item_waste_list, parent, false)
        val holder: ViewHolder = ViewHolder(view)
        holder.itemRoot.setOnClickListener {
            listener?.onItemClick(
                holder.bindingAdapterPosition,
                list.get(holder.bindingAdapterPosition),
            )
        }
        return holder
    }

    override fun onBindViewHolder(holder: WasteListAdapter.ViewHolder, position: Int) {
        holder.setData(position,list.get(position))
    }

    override fun getItemCount(): Int = list.size

    inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        val itemRoot: LinearLayout = itemView.findViewById(R.id.item_root)
        private val tvName: TextView = itemView.findViewById(R.id.tv_name)
        private val tvType: TextView = itemView.findViewById(R.id.tv_type)
        private val tvCode: TextView = itemView.findViewById(R.id.tv_code)
        private val vLine: LineView = itemView.findViewById(R.id.v_line)
        fun setData(position: Int,data: WasteListEntity.DataList) {
            with(data) {
                tvName.text = UIUtils.getValue(wasteName)
                tvType.text = UIUtils.getValue(wasteType)
                tvCode.text = UIUtils.getValue(wasteCode)
                val mod = Math.floorMod(position, list.size)
                vLine.setShapeDrawable(getShapeColor(mod))
            }
        }
    }

    interface OnClickListener {
        fun onItemClick(pos: Int = 0, item: WasteListEntity.DataList?)
    }

    fun getShapeColor(mod: Int): Int {
        return when(mod) {
            0 -> com.maple.common.R.drawable.shape_line_view_0
            1 -> com.maple.common.R.drawable.shape_line_view_1
            2 -> com.maple.common.R.drawable.shape_line_view_2
            3 -> com.maple.common.R.drawable.shape_line_view_3
            4 -> com.maple.common.R.drawable.shape_line_view_4
            5 -> com.maple.common.R.drawable.shape_line_view_5
            6 -> com.maple.common.R.drawable.shape_line_view_6
            7 -> com.maple.common.R.drawable.shape_line_view_7
            8 -> com.maple.common.R.drawable.shape_line_view_8
            9 -> com.maple.common.R.drawable.shape_line_view_9
            else -> com.maple.common.R.drawable.shape_line_view
        }
    }

}